/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode log = headA;
        ListNode sot = headB;
        int lenA = 0;
        int lenB = 0;
        while(log != null) {
            log = log.next;
            lenA ++;
        }
        while(sot != null) {
            sot = sot.next;
            lenB ++;
        }
        int len = lenA - lenB;
        if(len < 0) {
            len = lenB - lenA;
            log = headB;
            sot = headA;
        } else {
            log = headA;
            sot = headB;
        }
        while(len != 0) {
            log = log.next;
            len--;
        }
        while(log != sot) {
            log = log.next;
            sot = sot.next;
        }
        return log;
    }
}
